package com.blockmeta.bbs.businesslibrary.widget.chartview.Axis;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;

import com.blankj.utilcode.util.TimeUtils;
import com.blockmeta.bbs.baselibrary.utils.DimentionsUtil;
import com.blockmeta.bbs.baselibrary.utils.FormatUtil;
import com.blockmeta.bbs.businesslibrary.websocket.Symbol;
import com.blockmeta.bbs.businesslibrary.widget.chartview.CandleData;
import com.blockmeta.bbs.businesslibrary.widget.chartview.ChartData;
import com.blockmeta.bbs.businesslibrary.widget.chartview.CoinChart;
import com.blockmeta.bbs.businesslibrary.widget.chartview.Layer.Layer;
import com.blockmeta.bbs.businesslibrary.widget.chartview.Tools.ChartConverter;

import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import static com.blockmeta.bbs.businesslibrary.widget.chartview.ChartData.ChartPeroid.CP_1min;
import static com.blockmeta.bbs.businesslibrary.widget.chartview.ChartData.ChartPeroid.CP_5min;

public class Coordinate
{
	public static float UPDOWNSPACE = 28.0f;
	protected Axis m_xAxis;
	protected Axis m_yAxis;
	private RectF m_rectfLayerRect;
	private RectF m_rectfChartRect;
	private float m_fXFirstPosition = 0.f;
	private float m_fXInterval = 0.f;
	private float m_fYFirstPosition = 0.f;
	private float m_fYInterval = 0.f;
	protected int m_nYIntervalCount = 0;
	protected float m_nYIntervalStandard = 0.f;
	protected float m_fMaxValue = 0.f;//经过和均线，还有为了好的间距而计算的一个坐标最大最小值
	protected float m_fMinValue = 0.f;
	protected int m_KMaxValueIndex=0;
	protected int m_KMinValueIndex=0;
	private CoinChart.PointLocation m_originPointLocation = CoinChart.PointLocation.BOTTOMRIGHT;
	public CoordinateCallback m_callback;
	protected int m_nStartIndex = 0;
	protected float m_ScaleFactor = 0.f;
	protected int m_nQueryCount = 0;
	private Object m_objLock = new Object();

	private float m_fXTextSpace = 0.f;
	private float m_fYTextSpace = 0.f;
	protected int m_bDigit = 2;
	public Symbol tSymBol;

	public SimpleDateFormat m_forDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
	public SimpleDateFormat m_hourDateFormat = new SimpleDateFormat("HH:mm");
	public SimpleDateFormat m_Format = new SimpleDateFormat("MM/dd");
	public SimpleDateFormat m_DateTimeFormat = new SimpleDateFormat("MM/dd HH:mm");
	public SimpleDateFormat m_YearFormat = new SimpleDateFormat("yyyy/MM/dd");
	public SimpleDateFormat m_5YearFormat = new SimpleDateFormat("yyyy/MM");
	public int mXTagValueCount = 10;//默认10个值一个坐标

	private Axis.AxisCallback m_callbackAxis = new Axis.AxisCallback()
	{
		@Override
		public RectF getChartRectF()
		{
			return m_rectfLayerRect;
		}

		@Override
		public float getXTextSpace()
		{
			return Coordinate.this.getXTextSpace();
		}

		@Override
		public float getYTextSpace()
		{
			return Coordinate.this.getYTextSpace();
		}

		@Override
		public Rect getPanelRect()
		{
			return m_callback.getPanelRect();
		}

		@Override
		public CoinChart.PointLocation getOriginPointLocation()
		{
			return m_originPointLocation;
		}

		@Override
		public boolean isCoordinateTextOuterY()
		{
			return m_callback.isCoordinateTextOuterY();
		}

		@Override
		public float getYesterdayClose()
		{
			return m_callback.getYesterClose();
		}

		@Override
		public boolean isOnlyShowSquareNum()
		{
			return m_callback.isCOnlyShowSquareNum();
		}

		@Override
		public boolean isNeedUpDownSpace()
		{
			return m_callback.isNeedUpDownSpace();
		}

		@Override
		public Layer.ChartTypeEnum getCharTypeEnum()
		{
			return m_callback.getCharTypeEnum();
		}

	};

	public Coordinate()
	{
		m_xAxis = new XAxis();
		m_xAxis.setCallback(m_callbackAxis);
		m_yAxis = new YAxis();
		m_yAxis.setCallback(m_callbackAxis);
		m_rectfLayerRect = new RectF(0.f, 0.f, 0.f, 0.f);
	}

	public float getXTextSpace()
	{
		return m_fXTextSpace;
	}

	public float getYTextSpace()
	{
		return m_fYTextSpace;
	}

	public void isShowXTitle(boolean pShow)
	{
		m_xAxis.isShowTitle(pShow);
	}

	public boolean isShowXTitle()
	{
		return m_xAxis.isShowTitle();
	}

	public void isShowYTitle(boolean pShow)
	{
		m_yAxis.isShowTitle(pShow);
	}

	public boolean isShowYTitle()
	{
		return m_yAxis.isShowTitle();
	}

	public float getY0Value()
	{
		return m_yAxis.m_fy0Value;
	}

	public void setXTitle(String pTitle)
	{
		m_xAxis.setTitle(pTitle);
	}

	public void setYTitle(String pTitle)
	{
		m_yAxis.setTitle(pTitle);
	}

	public void setData(ChartData pData)
	{

//		FDTLogUtils.d("tingchart","setData m_nStartIndex"+m_nStartIndex);

		List<Point> tXList = new ArrayList<Point>();
		List<Point> tYList = new ArrayList<Point>();

		if (pData != null)
		{
			ChartData.ChartPeroid tChartType = pData.getChartPeroid();
			Layer.ChartTypeEnum tLayerType = m_callback.getCharTypeEnum();

			List<CandleData> tDataList = pData.getCandleData();
			int tDataCount = tDataList.size();

			m_nQueryCount = tChartType.getQueryCount();//获取相应的时间周期的chart的查询获得的根数
			if (tChartType == ChartData.ChartPeroid.PERFORMANCE)
			{
				m_nQueryCount = tDataCount;//该类型下查询返回的根数为数据的总数
			}
			else
			{
				if (pData.getCount() == -1)
				{
					m_nQueryCount = (m_nQueryCount == 0 || tDataCount < m_nQueryCount) ? tDataCount : m_nQueryCount;
				}
				else
				{
					m_nQueryCount = (tDataCount < pData.getCount()) ? tDataCount : pData.getCount();
				}
			}
			int tStartIndex = tDataCount - m_nQueryCount;
			m_nStartIndex = tStartIndex;
			int nXTagCount = m_callback.getXTagCount();
			int tXTagCount = 0;

			{
				tXTagCount = m_nQueryCount < nXTagCount ? 1 : (m_nQueryCount / nXTagCount);
			}


			boolean tSimpleMode = m_callback.isSimpleMode();
			int tDifCount = m_nQueryCount < nXTagCount ? 0 : m_nQueryCount % nXTagCount;


			Date tFlagDate = null;

			tSymBol = pData.getSymbol();

			float tMaxValue = 0.f;
			float tMinValue = 0.f;

			int tTempCount = 0;
			for (int i = tStartIndex; i < tDataCount; i++)
			{
				CandleData tData = tDataList.get(i);

				Date tDate = null;
				try
				{
					tDate = TimeUtils.millis2Date(tData.m_nDate);
				}
				catch (Exception e1)
				{
					tStartIndex++;
					e1.printStackTrace();
					continue;
				}

				float tHigh = -1;
				float tLow = -1;
				if (tSimpleMode)
				{
					tHigh = (float) tData.getDbClose();
					tLow = (float) tData.getDbClose();
				}
				else if (tLayerType == Layer.ChartTypeEnum.AREA)
				{
					tHigh = (float) tData.getDbClose();
					tLow = (float) tData.getDbClose();
				}
				else if (tLayerType == Layer.ChartTypeEnum.BAR)
				{
					tHigh = (float) tData.m_lVolume;
					tLow = (float) tData.m_lVolume;
				}
//				else if(tChartType == ChartPeroid.CP_DC && (tLayerType == ChartTypeEnum.DAILY || tLayerType == ChartTypeEnum.LINE || tLayerType == ChartTypeEnum.MAMoveLine))//如果是分时，那么只有close
//				{
//					tHigh = (float) tData.m_dbClose;
//					tLow = (float) tData.m_dbClose;
//					FDTLogUtils.d("tingchart", tChartType +"CP_DC tHigh"+tHigh+" tLow"+tLow);
//				}
				else
				{
					tHigh = (float) tData.getDbHigh();
					tLow = (float) tData.getDbLow();
				}

				if (i == tStartIndex)
				{
					tMaxValue = tHigh;
					tMinValue = tLow;
					tFlagDate = getFirstDate(tChartType, tDate);
					m_KMaxValueIndex=i;
					m_KMinValueIndex=i;
				}
				else
				{
					if (tHigh >= tMaxValue)
					{
						tMaxValue = tHigh;
						m_KMaxValueIndex=i;
					}
					if (tLow <= tMinValue)
					{
						tMinValue = tLow;
						m_KMinValueIndex=i;
					}
				}

				if (tSimpleMode)
				{
					if ((i - tStartIndex) % tXTagCount == tDifCount)
					{
						String tXText = null;

						Calendar tCal = Calendar.getInstance();
						tCal.setTime(tDate);

						if (tChartType == CP_1min)
						{
							tXText = m_hourDateFormat.format(tDate);//  String.format("%02d", CommonLib.getHour(tData.m_nTime)) + ":" + String.format("%02d", CommonLib.getMinute(tData.m_nTime));
						}
						else if (tChartType == CP_5min)
						{
							int tHour = tCal.get(Calendar.HOUR_OF_DAY);
							if (tHour == 0)
							{
								tXText = m_Format.format(tDate);
							}
							else
							{
								tXText = m_hourDateFormat.format(tDate);
							}
						}
						else if (tChartType == ChartData.ChartPeroid.CP_1week)
						{
							tXText = m_YearFormat.format(tDate);
						}
						else if (tChartType == ChartData.ChartPeroid.CP_1month)
						{
							tXText = m_5YearFormat.format(tDate);
						}
						else if (tChartType == ChartData.ChartPeroid.CP_DC)
						{
							tXText = m_hourDateFormat.format(tDate);
						}
						else
						{
							tXText = m_Format.format(tDate);
						}

						tXList.add(new Point(0, 0, 0, 0, true, tXText, (i + 1 - tStartIndex)));
					}
				}
				else if (tDate.getTime() >= tFlagDate.getTime())
				{
					String tXText = null;

					Calendar tCal = Calendar.getInstance();
					tCal.setTime(tFlagDate);

					if (tChartType == CP_1min)
					{
						tXText = m_hourDateFormat.format(tDate);
					}
					else if (tChartType == CP_5min)
					{
						int tHour = tCal.get(Calendar.HOUR_OF_DAY);
						if (tHour == 0)
						{
							tXText = m_Format.format(tDate);
						}
						else
						{
							tXText = m_hourDateFormat.format(tDate);
						}
					}
					else if (tChartType == ChartData.ChartPeroid.CP_1week)
					{
						tXText = m_YearFormat.format(tDate);
					}
					else if (tChartType == ChartData.ChartPeroid.CP_1month)
					{
						tXText = m_5YearFormat.format(tDate);
					}
					else
					{
						tXText = m_Format.format(tDate);
					}

					tXList.add(new Point(0, 0, 0, 0, true, tXText, (i + 1 - tStartIndex)));
					tFlagDate = getNextDate(tChartType, tDate);
				}
			}

			if (m_callback != null && m_callback.isShowYesterClose()
					&& (tSimpleMode || tLayerType != Layer.ChartTypeEnum.BAR))
			{
				float tYesterClose = m_callback.getYesterClose();
				if (tYesterClose > tMaxValue)
				{
					tMaxValue = tYesterClose;
				}
				else if (tYesterClose < tMinValue)
				{
					tMinValue = tYesterClose;
				}
			}

//			LogUtils.d("chen","maxIndex:"+m_KMaxValueIndex+",minIndex:"+m_KMinValueIndex);

			m_bDigit = 2;
			if (tSymBol != null)
			{
				m_bDigit = tSymBol.getDigit();
			}

			float tRate = (float) Math.pow(10, m_bDigit);

			float tTempStandard = 5.f / tRate;

			if (tSimpleMode)
			{
				m_nYIntervalCount = 5;
				int tStandard = 5;
				int nMaxValue = (int) (tMaxValue * tRate);
				int nMinValue = (int) (tMinValue * tRate);

				nMaxValue = ((int) (nMaxValue / tStandard) + (nMaxValue % tStandard == 0 ? 0 : 1)) * tStandard;
				nMinValue = ((int) (nMinValue / tStandard)) * tStandard;

				int tValueDist = nMaxValue - nMinValue;

				while (((tValueDist / tStandard) % m_nYIntervalCount) != 0)
				{
					nMaxValue += tStandard;
					tValueDist += tStandard;
				}

				tMaxValue = nMaxValue / tRate;
				tMinValue = nMinValue / tRate;
				tTempStandard = (tValueDist / m_nYIntervalCount) / tRate;
				m_nYIntervalStandard = tTempStandard;

				for (int i = 0; i <= m_nYIntervalCount; i++)
				{
					String tCurrentValue = FormatUtil.getFormatString(tMinValue + tTempStandard * i, m_bDigit);
					tYList.add(new Point(0, 0, 0, 0, true, tCurrentValue, (i + 1)));
				}
			}
			else if (tChartType == ChartData.ChartPeroid.PERFORMANCE)
			{
				float tBaseLV1 = 5f;
				float tBaseLV2 = 0.5f;
				float tBaseLV3 = 0.05f;
				int tYTagCount = 10;

				float tPattern = 0.f;
				if (tMinValue >= 1)
				{
					tPattern = tBaseLV1;
				}
				else if (tMinValue >= 0.1 && tMinValue < 1)
				{
					tPattern = tBaseLV2;
				}
				else
				{
					tPattern = tBaseLV3;
				}

				if (tMaxValue == tMinValue)
				{
					tMaxValue = (tMaxValue < 0) ? (int) (tMaxValue / tPattern) * tPattern : ((int) (tMaxValue / tPattern) + 1) * tPattern;
					tMinValue = (tMinValue < 0) ? ((int) (tMinValue / tPattern) - 1) * tPattern : ((int) (tMinValue / tPattern)) * tPattern;
				}

				float tYMin = (tMinValue < 0) ? ((int) (tMinValue / tPattern) - 1) * tPattern : ((int) (tMinValue / tPattern)) * tPattern;
				float tYStep = (tMaxValue - tYMin) / (tYTagCount - 1);

				if (tYStep >= 1)
				{
					int nAns = (int) (tYStep / tBaseLV1);
					tYStep = (tYStep - tBaseLV1 * nAns != 0) ? ((int) (tYStep / tBaseLV1) + 1) * tBaseLV1 : tYStep;
				}
				else if (tYStep >= 0.1 && tYStep < 1)
				{
					tYStep = tYStep * 10;
					int nAns = (int) (tYStep / tBaseLV1);
					tYStep = (tYStep - tBaseLV1 * nAns != 0) ? ((int) (tYStep / tBaseLV1) + 1) * tBaseLV1 / 10.f : tYStep / 10.0f;
				}
				else
				{
					tYStep = Float.valueOf(String.format(Locale.US, "%.03f", tYStep)).floatValue();
					tYStep = tYStep * 100;
					int nAns = (int) (tYStep / tBaseLV1);

					tYStep = (tYStep - tBaseLV1 * nAns != 0) ? (((int) (tYStep / tBaseLV1)) + 1) * tBaseLV1 / 100.f : tYStep / 100.f;
				}

				m_nYIntervalCount = tYTagCount - 1;
				tTempStandard = tYStep;
				tMinValue = tYMin;
				tMaxValue = tYStep * m_nYIntervalCount + tMinValue;
				for (int i = 0; i <= m_nYIntervalCount; i++)
				{
					if (i == 0 || i == m_nYIntervalCount)
					{
						tYList.add(new Point(0, 0, 0, 0, true, "", (i + 1)));
					}
					else
					{
						String tCurrentValue = FormatUtil.getFormatString(tMinValue + tTempStandard * i, m_bDigit);
						tYList.add(new Point(0, 0, 0, 0, true, tCurrentValue, (i + 1)));
					}
				}
			}
			else if (tLayerType == Layer.ChartTypeEnum.BAR)
			{
				m_nYIntervalCount = 2;
				m_nYIntervalStandard = (tMaxValue - tMinValue) / 2;

				if (tMaxValue > 0)
				{
					long maxValue = (long) tMaxValue;
					long midValue = maxValue >> 1;

					String max = FormatUtil.longToVolumeHumanReadableFormat(maxValue, 0);
					String mid = FormatUtil.longToVolumeHumanReadableFormat(midValue, 0);

					tYList.add(new Point(0, 0, 0, 0, true, "", 1));
					tYList.add(new Point(0, 0, 0, 0, true, mid, 2));
					tYList.add(new Point(0, 0, 0, 0, true, max, 3));
				}
			}
//			else if(tChartType == ChartPeroid.CP_DC && (tLayerType == ChartTypeEnum.DAILY || tLayerType == ChartTypeEnum.MAMoveLine))//如果是分时图的画，要把昨结价显示在中间，并且要绘制百分比
//			{
//				FDTLogUtils.d("tingchart","CP_DC setdata tMaxValue"+tMaxValue+" tMinValue"+tMinValue);
//				float tYesterClose = m_callback.getYesterClose();
//				boolean updist = Math.abs(tMaxValue - tYesterClose) > Math.abs(tMinValue - tYesterClose) ? true : false;
//				if(updist)
//				{
//					tMinValue = tYesterClose - Math.abs(tMaxValue - tYesterClose);
//				}
//				else
//				{
//					tMaxValue = tYesterClose + Math.abs(tMinValue - tYesterClose);
//				}
//
//				m_nYIntervalCount = 6;
//				int tStandard = 5;
//				int nMaxValue = (int) (tMaxValue * tRate);
//				int nMinValue = (int) (tMinValue * tRate);
//
//				nMaxValue = ((int) (nMaxValue / tStandard) + (nMaxValue % tStandard == 0 ? 0 : 1)) * tStandard;
//				nMinValue = ((int) (nMinValue / tStandard)) * tStandard;
//
//				int tValueDist = nMaxValue - nMinValue;
//
//				while (((tValueDist / tStandard) % m_nYIntervalCount) != 0)
//				{
//					nMaxValue += tStandard;
//					tValueDist += tStandard;
//				}
//
//				tMaxValue = nMaxValue / tRate;
//				tMinValue = nMinValue / tRate;
//				tTempStandard = (tValueDist / m_nYIntervalCount) / tRate;
//				m_nYIntervalStandard = tTempStandard;
//
//				for (int i = 0; i <= m_nYIntervalCount; i++)
//				{
//					String tCurrentValue = CommonLib.getFormatString(tMinValue + tTempStandard * i, m_bDigit);
//					tYList.add(new Point(0, 0, 0, 0, true, tCurrentValue, (i + 1)));
//				}
//			}
			else
			{
				int tStandard = 10;
				int nMaxValue = (int) (tMaxValue * tRate);
				int nMinValue = (int) (tMinValue * tRate);

				nMaxValue = (nMaxValue / tStandard + (nMaxValue % tStandard == 0 ? 0 : 1)) * tStandard;
				nMinValue = (nMinValue / tStandard) * tStandard;

				int tValueDist = nMaxValue - nMinValue;

				int tTemp = 5;

				while (tValueDist / tTemp > 10)
				{
					tTemp *= 2;
				}

				m_nYIntervalCount = tValueDist / tTemp;

				tMaxValue = nMaxValue / tRate;
				tMinValue = nMinValue / tRate;
				tTempStandard = tTemp / tRate;
				m_nYIntervalStandard = tTempStandard;

				for (int i = 0; i <= m_nYIntervalCount; i++)
				{
					String tCurrentValue = FormatUtil.getFormatString(tMaxValue - tTempStandard * (m_nYIntervalCount - i), m_bDigit);
					tYList.add(new Point(0, 0, 0, 0, true, tCurrentValue, (i + 1)));
				}
			}

			m_fMaxValue = tMaxValue;
			m_fMinValue = tMinValue;
		}

		m_xAxis.setPointList(tXList);
		m_yAxis.setPointList(tYList);
	}

	public void drawCoordinate(Canvas pCanvas, Paint pPaint, ChartData pData)
	{
		List<Point> tXList = m_xAxis.getPointList();
		List<Point> tYList = m_yAxis.getPointList();

		int tXListSize = tXList.size();
		int tYListSize = tYList.size();

		Layer.ChartTypeEnum tLayerType = m_callback.getCharTypeEnum();

		{
			for (int ii = 0; ii < tXListSize; ii++)
			{

				Point tPoint = tXList.get(ii);
				float tXPoint = tPoint.getIntervalCount() * m_fXInterval + m_rectfLayerRect.left;
				tPoint.setStartY(m_rectfLayerRect.bottom);
				tPoint.setEndY(m_rectfLayerRect.top);
				tPoint.setStartX(tXPoint);
				tPoint.setEndX(tXPoint);
			}

			for (int ii = 0; ii < tYListSize; ii++)
			{
				Point tPoint = tYList.get(ii);
				float tYPoint = m_fYFirstPosition - ii * m_fYInterval;
				tPoint.setStartY(tYPoint);
				tPoint.setEndY(tYPoint);
				tPoint.setStartX(m_rectfLayerRect.right);
				tPoint.setEndX(m_rectfLayerRect.left);
			}

			m_xAxis.drawAxis(pCanvas, pPaint);
			m_yAxis.drawAxis(pCanvas, pPaint);
		}

	}

	public void drawCoordinateText(Canvas pCanvas, Paint pPaint, ChartData pData)
	{
		m_xAxis.drawAxisText(pCanvas, pPaint);
		m_yAxis.drawAxisText(pCanvas, pPaint);
	}

	public void drawOutRect(Canvas pCanvas, Paint pPaint)
	{
		pPaint.setColor(CoinChart.getBorderColor());
		pPaint.setAntiAlias(true);
		pPaint.setStrokeWidth(1f);
		pPaint.setStyle(Paint.Style.STROKE);
		pCanvas.drawRect(new RectF(m_rectfLayerRect.left, m_rectfLayerRect.top, m_rectfLayerRect.right, m_rectfLayerRect.bottom), pPaint);
	}

	private void drawScrollAxis(Canvas pCanvas, Paint pPaint, ChartData pData, List<Point> tXList, List<Point> tYList)
	{
		int tYListSize;
		if(pData == null)
		{
			return;
		}
		tYList.clear();
		int startIndex = getStartIndex();//获取当前移动到的位置
		int tagXCount = getTagXCount(); //获取一个坐标显示的数据数量
		int onePageCount = getOnePageXCount();//获取一页显示的数量

		int startXTagIndex = 0;
		if (startIndex % tagXCount == 0)//计算移动后第一个显示的x坐标
		{
			startXTagIndex = startIndex / tagXCount;
		}
		else
		{
			startXTagIndex = startIndex / tagXCount + 1;
		}

		//当前要显示多少个xtag，最多x的坐标和没有超过最多的情况下要显示多少个x坐标
		int remainXTagCount = (startIndex + getOnePageXCount())/getTagXCount() + 1 - startXTagIndex;
//			FDTLogUtils.d("tingting5","tXList.size()"+tXList.size()+"startIndex + getOnePageXCount()"+(startIndex + getOnePageXCount())+"getTagXCount()"+getTagXCount()+" "+"startXTagIndex"+startXTagIndex+" "+tLayerType+"remainXTagCount"+remainXTagCount);
		int max = Math.min(startXTagIndex+remainXTagCount,tXList.size());
		for (int ii = startXTagIndex; ii < max; ii++)
		{
			Point tPoint = tXList.get(ii);
			float tXPoint = (tPoint.getIntervalCount() - startIndex) * m_fXInterval + m_rectfLayerRect.left;
			tPoint.setStartY(m_rectfLayerRect.bottom);
			tPoint.setEndY(m_rectfLayerRect.top);
			tPoint.setStartX(tXPoint);
			tPoint.setEndX(tXPoint);
		}
		m_xAxis.drawAxis(pCanvas, startXTagIndex, remainXTagCount, pPaint);


		//计算要显示的数据中的最大值和最小值
		List<CandleData> tDataList = pData.getCandleData();
		int tDataCount = tDataList.size();
		float tHigh = -1;
		float tLow = -1;
		float tMaxValue = -1;
		float tMinValue = -1;
		CandleData tData;
		int tmax = Math.min(startIndex + onePageCount,tDataList.size());
		for (int i = startIndex; i < tmax; i++)
		{
			tData = tDataList.get(i);
			tHigh = (float) tData.getDbHigh();
			tLow = (float) tData.getDbLow();

			if (i == startIndex)
			{
				tMaxValue = tHigh;
				tMinValue = tLow;
			}
			else
			{
				if (tHigh > tMaxValue)
				{
					tMaxValue = tHigh;
				}
				if (tLow < tMinValue)
				{
					tMinValue = tLow;
				}
			}
		}
		if(tMinValue > 0)//保证有0
		{
			tMinValue = 0;
		}
		if(tMaxValue < 0)
		{
			tMaxValue = 0;
		}
		int tYTagCount = getTagYCount();
		int tPattern = 0;

		DecimalFormat df = new DecimalFormat(",##0");
//			DecimalFormat df2 = new DecimalFormat(",##0.#");
		df.setRoundingMode(RoundingMode.HALF_UP);
		for (int j = 0; j < Integer.MAX_VALUE; j++)
		{
			tPattern += 100;
			int tYMin = 0;
			int tYMax = 0;
			if (tMinValue % tPattern == 0)
			{
				tYMin = (int) tMinValue;
			}
			else
			{
				tYMin = ((int) (tMinValue / tPattern) - 1) * tPattern;
			}
			if (tMaxValue % tPattern == 0)
			{
				tYMax = (int) tMaxValue;
			}
			else
			{
				tYMax = ((int) (tMaxValue / tPattern) + 1) * tPattern;
			}
			if (Math.abs(tYMax - tYMin) / tPattern <= (tYTagCount - 1))
			{
				tMaxValue = tYMax;
				tMinValue = tYMin;

				m_nYIntervalStandard = tPattern;

				m_nYIntervalCount = tYTagCount - 1;
				m_fMinValue = tMinValue;
				m_fMaxValue = tMinValue + tPattern * (tYTagCount - 1);
				for (int x = 0; x < tYTagCount; x++)
				{
					if(Math.abs(tYMin) < 10000)
					{
						String strValue = df.format(tYMin);
						tYList.add(new Point(0, 0, 0, 0, true, strValue, x));
					}else {
						float pValue = (float)tYMin / 1000;
						String strValue = df.format(pValue);
						tYList.add(new Point(0, 0, 0, 0, true, strValue + "k", x));
					}

					tYMin += tPattern;
				}


				break;
			}
		}

		tYListSize = tYList.size();
		for (int ii = 0; ii < tYListSize; ii++)
		{
			Point tPoint = tYList.get(ii);
			float tYPoint = m_fYFirstPosition - ii * m_fYInterval;
			tPoint.setStartY(tYPoint);
			tPoint.setEndY(tYPoint);
			tPoint.setStartX(m_rectfLayerRect.right);
			tPoint.setEndX(m_rectfLayerRect.left);
		}

		m_yAxis.drawAxis(pCanvas, pPaint);
	}

	public void initialize(RectF pWeight, RectF pChartPanel, ChartData.ChartPeroid pType, RectF pSpace, float textSpaceX, float textSpaceY)
	{

		m_rectfLayerRect = ChartConverter.ConvertToRect(pWeight, pChartPanel, pSpace);
		m_rectfChartRect = new RectF(pSpace.left, pSpace.top,
				pSpace.left + pChartPanel.width(), pSpace.top + pChartPanel.height());

//		if (m_originPointLocation == PointLocation.BOTTOMRIGHT)
//		{
//			m_fXTextSpace = pSpace.bottom;
//			m_fYTextSpace = pSpace.right;
//		}
//		else if (m_originPointLocation == PointLocation.BOTTOMLEFT)
//		{
//			m_fXTextSpace = pSpace.bottom;
//			m_fYTextSpace = pSpace.left;
//		}
//		else if (m_originPointLocation == PointLocation.TOPRIGHT)
//		{
//			m_fXTextSpace = pSpace.top;
//			m_fYTextSpace = pSpace.right;
//		}
//		else if (m_originPointLocation == PointLocation.TOPLEFT)
//		{
//			m_fXTextSpace = pSpace.top;
//			m_fYTextSpace = pSpace.left;
//		}

		m_fXTextSpace = textSpaceX;
		m_fYTextSpace = textSpaceY;
		Layer.ChartTypeEnum tLayerType = m_callback.getCharTypeEnum();

		float tXInterval = (m_rectfLayerRect.width()) / (m_nQueryCount + 1);
		m_fXInterval = tXInterval;

		List<Point> tYList = m_yAxis.getPointList();
		int tYListSize = tYList.size();

		UPDOWNSPACE = (float) (m_rectfLayerRect.height() / 5.0);

		if(pType == ChartData.ChartPeroid.DEPTH || pType == ChartData.ChartPeroid.PERFORMANCE)
		{
			tXInterval = (m_rectfLayerRect.width()) / (m_nQueryCount - 1);
			m_fXInterval = tXInterval;
			m_fYInterval = m_rectfLayerRect.height() / (m_nYIntervalCount/* + 0.4f*/);
			m_fYFirstPosition = tYListSize > 0 ? m_rectfLayerRect.bottom/* - (m_rectfLayerRect.height() - m_fYInterval * m_nYIntervalCount) / 2.f */ : m_rectfLayerRect.bottom;
			m_fXFirstPosition = m_rectfLayerRect.left;
		}
		else
		{
			m_fYInterval = m_rectfLayerRect.height() * m_nYIntervalStandard / (m_fMaxValue - m_fMinValue);
			m_fYFirstPosition = tYListSize > 0 ? m_rectfLayerRect.bottom - (m_rectfLayerRect.height() - m_fYInterval * m_nYIntervalCount)/2 :
					m_rectfLayerRect.bottom;
			m_fXFirstPosition = m_rectfLayerRect.left + m_fXInterval;
		}


		float bigTextSize = DimentionsUtil.dp2px(10.f);
		float tTextSize = bigTextSize;
//		if(m_fXTextSpace > spaceSize)
//		{
//			tTextSize = bigTextSize;
//		}
//		else
//		{
//			float tXTextPadding = m_fXTextSpace / 8;
//			float tYTextPadding = m_fYTextSpace / 6;
////
////		float maxTextSize = DimentionsUtil.dp2px(10.f);
//
//			float tYWidth = m_fYTextSpace - tYTextPadding * 2;
//			float tXWidth = m_fXTextSpace - tXTextPadding * 2;
//			for (int ii = 0; ii < tYListSize; ii++)
//			{
//				Point tPoint = tYList.get(ii);
//
//				if (!tPoint.getText().equals(""))
//				{
//					float tTempTextSize = CommonLib.determineMaxTextSize(tPoint.getText(), tYWidth);
//					tTextSize = tTempTextSize >= tXWidth ? tXWidth : tTempTextSize;
//				}
//			}
//			tTextSize = smallTextSize;
//		}

		m_xAxis.setTextSize(tTextSize);
		m_yAxis.setTextSize(tTextSize);
	}

	public int getDigit()
	{
		return m_bDigit;
	}

	public float getXFirstPosition()
	{
		return m_fXFirstPosition;
	}

	public float getXInterval()
	{
		return m_fXInterval;
	}

	public float getYFirstPosition()
	{
		return m_fYFirstPosition;
	}

	public float getValueHeight()
	{
//		if(m_callback.isNeedUpDownSpace())
//		{
//			return getLayerRectF().bottom - getLayerRectF().top - UPDOWNSPACE;
//		}
//		else
		{
			return getLayerRectF().bottom - getLayerRectF().top;
		}
	}

	public float getYInterval()
	{
		return m_fYInterval;
	}

	public int getYIntervalCount()
	{
		return m_nYIntervalCount;
	}

	public float getMaxValue()
	{
		return m_fMaxValue;
	}

	public float getMinValue()
	{
		return m_fMinValue;
	}

	public int getKMaxValueIndex()
	{
		return m_KMaxValueIndex;
	}

	public int getKMinValueIndex()
	{
		return m_KMinValueIndex;
	}

	public int getKMaxDateIndex(){
		return m_KMaxValueIndex;
	}

	public int getKMinDateIndex(){
		return m_KMinValueIndex;
	}



	public RectF getLayerRectF()
	{
		return m_rectfLayerRect;
	}

	public RectF getChartRectF()
	{
		return m_rectfChartRect;
	}

	public void setOriginPointLocation(CoinChart.PointLocation pLocation)
	{
		m_originPointLocation = pLocation;
	}

	public CoinChart.PointLocation getOriginPointLocation()
	{
		return m_originPointLocation;
	}

	public int getStartIndex()
	{
		return m_nStartIndex;
	}

	public boolean moveStartIndex(int pdist)//如果剩下的不足pdist就把剩下的全部移动
	{
		if ((m_nStartIndex > 0 && pdist < 0) || ((m_nQueryCount - getOnePageXCount() - m_nStartIndex) > 0 && pdist > 0))
		{
			if (pdist > 0)
			{
				m_nStartIndex += (m_nQueryCount - getOnePageXCount() - m_nStartIndex) >= pdist ? pdist : (m_nQueryCount - getOnePageXCount() - m_nStartIndex);
			}
			if (pdist < 0)
			{
				m_nStartIndex += Math.abs(m_nStartIndex) >= Math.abs(pdist) ? pdist : -m_nStartIndex;
			}
			return true;
		}

		return false;
	}

	public boolean canMoveIndex(int pdist)
	{
		if((m_nStartIndex > 0 && pdist < 0) || ((m_nQueryCount - getOnePageXCount() - m_nStartIndex) > 0 && pdist > 0 ))
		{
			return true;
		}
		return false;
	}

	/**
	 * 获取一页显示的数据条数
	 *
	 * @return
	 */
	public int getOnePageXCount()
	{
		//如果当前数据量小于68，就全部显示，如果大于就显示68个
//		int maxNum = 68;
//		if(m_nQueryCount > 0 && m_nQueryCount <= maxNum)
//		{
//			return m_nQueryCount;
//		}else if(m_nQueryCount > 0 && m_nQueryCount > maxNum)
//		{
//			return maxNum;
//		}
//		return 26;

		//如果大于12，返回querycount，否则为12
		int minNum = 12;
		if(m_nQueryCount > minNum)
		{
			return m_nQueryCount;
		}
		return minNum;
	}


	/**
	 * 获取一个ytag坐标包括多少数据条数
	 *
	 * @return
	 */
	public int getTagYCount()
	{
		return 7;
	}

	public Object getLockObject()
	{
		return m_objLock;
	}

	public Date getFirstDate(ChartData.ChartPeroid pType, Date pDate)
	{
		Date tResult = null;
		Calendar tCla = Calendar.getInstance();
		tCla.setTime(pDate);
		int tYear = tCla.get(Calendar.YEAR);
		int tMonth = tCla.get(Calendar.MONTH);
		int tDay = tCla.get(Calendar.DATE);
		int tHour = tCla.get(Calendar.HOUR_OF_DAY);
		int tMinute = tCla.get(Calendar.MINUTE);
		switch (pType)
		{
			case CP_1min:
			{

				if (tMinute == 0)
				{
					tMinute = 0;
				}
				else if (tMinute >= 0 && tMinute < 15)
				{
					tMinute = 15;
				}
				else if (tMinute >= 0 && tMinute < 30)
				{
					tMinute = 30;
				}
				else if (tMinute >= 30 && tMinute < 45)
				{
					tMinute = 45;
				}
				else
				{
					tMinute = 0;
					tCla.add(Calendar.HOUR_OF_DAY, 1);
				}

				tCla.set(Calendar.MINUTE, tMinute);

//				if (tMinute == 0)
//				{
//					tMinute = 0;
//				}
//				else if (tMinute >= 0 && tMinute < 15)
//				{
//					tMinute = 15;
//				}
//				else if (tMinute >= 0 && tMinute < 30)
//				{
//					tMinute = 30;
//				}
//				else if (tMinute >= 30 && tMinute < 45)
//				{
//					tMinute = 45;
//				}
//				else
//				{
//					tMinute = 0;
//					tCla.add(Calendar.HOUR_OF_DAY, 1);
//				}
//
//				tCla.set(Calendar.MINUTE, tMinute);
				break;
			}
			case CP_5min:
			{
//
//				if (tMinute == 0)
//				{
//					tMinute = 0;
//				}
				if (tMinute == 0)
				{
					tMinute = 0;
				}
				else if(tMinute <= 60)
				{
					tMinute = 0;
					tCla.add(Calendar.HOUR_OF_DAY, 1);
				}



				tCla.set(Calendar.MINUTE, 0);

//				pDate = tCla.getTime();
//
//				if(tMinute == 0)
//				{
//					tCla.set(Calendar.HOUR_OF_DAY, tHour % 3 == 0 ? tHour : tHour +  3 - tHour % 3);
//				}
//				else
//				{
//					tHour++;
//					tCla.set(Calendar.HOUR_OF_DAY, tHour % 3 == 0 ? tHour : tHour +  3 - tHour % 3);
//				}

//				if (tHour >= 0 && tHour < 6)
//				{
//					if (tHour == 0 && tMinute == 0)
//					{
//						tCla.set(Calendar.HOUR_OF_DAY, 0);
//					}
//					else
//					{
//						tCla.set(Calendar.HOUR_OF_DAY, 6);
//					}
//				}
//				else if (tHour >= 6 && tHour < 12)
//				{
//					if (tHour == 6 && tMinute == 0)
//					{
//						tCla.set(Calendar.HOUR_OF_DAY, 6);
//					}
//					else
//					{
//						tCla.set(Calendar.HOUR_OF_DAY, 12);
//					}
//				}
//				else if (tHour >= 12 && tHour < 18)
//				{
//					if (tHour == 12 && tMinute == 0)
//					{
//						tCla.set(Calendar.HOUR_OF_DAY, 12);
//					}
//					else
//					{
//						tCla.set(Calendar.HOUR_OF_DAY, 18);
//					}
//				}
//				else
//				{
//					if (tHour == 18 && tMinute == 0)
//					{
//						tCla.set(Calendar.HOUR_OF_DAY, 18);
//					}
//					else
//					{
//						tCla.set(Calendar.HOUR_OF_DAY, 23);
//						tCla.setTimeInMillis(tCla.getTimeInMillis() + 60 * 60 * 1000);
//					}
//				}
//				tCla.set(Calendar.MINUTE, 0);
				break;
			}
			case CP_15min:
			{
				if (tHour >= 0 && tHour < 6)
				{
					if (tHour == 0 && tMinute == 0)
					{
						tCla.set(Calendar.HOUR_OF_DAY, 0);
					}
					else
					{
						tCla.set(Calendar.HOUR_OF_DAY, 6);
					}
				}
				else if (tHour >= 6 && tHour < 12)
				{
					if (tHour == 6 && tMinute == 0)
					{
						tCla.set(Calendar.HOUR_OF_DAY, 6);
					}
					else
					{
						tCla.set(Calendar.HOUR_OF_DAY, 12);
					}
				}
				else if (tHour >= 12 && tHour < 18)
				{
					if (tHour == 12 && tMinute == 0)
					{
						tCla.set(Calendar.HOUR_OF_DAY, 12);
					}
					else
					{
						tCla.set(Calendar.HOUR_OF_DAY, 18);
					}
				}
				else
				{
					if (tHour == 18 && tMinute == 0)
					{
						tCla.set(Calendar.HOUR_OF_DAY, 18);
					}
					else
					{
						tCla.set(Calendar.HOUR_OF_DAY, 23);
						tCla.setTimeInMillis(tCla.getTimeInMillis() + 60 * 60 * 1000);
					}
				}
				tCla.set(Calendar.MINUTE, 0);
				break;
			}
			case CP_30min:
			{
				if (tHour >= 0 && tHour < 6)
				{
					if (tHour == 0 && tMinute == 0)
					{
						tCla.set(Calendar.HOUR_OF_DAY, 0);
					}
					else
					{
						tCla.set(Calendar.HOUR_OF_DAY, 6);
					}
				}
				else if (tHour >= 6 && tHour < 12)
				{
					if (tHour == 6 && tMinute == 0)
					{
						tCla.set(Calendar.HOUR_OF_DAY, 6);
					}
					else
					{
						tCla.set(Calendar.HOUR_OF_DAY, 12);
					}
				}
				else if (tHour >= 12 && tHour < 18)
				{
					if (tHour == 12 && tMinute == 0)
					{
						tCla.set(Calendar.HOUR_OF_DAY, 12);
					}
					else
					{
						tCla.set(Calendar.HOUR_OF_DAY, 18);
					}
				}
				else
				{
					if (tHour == 18 && tMinute == 0)
					{
						tCla.set(Calendar.HOUR_OF_DAY, 18);
					}
					else
					{
						tCla.set(Calendar.HOUR_OF_DAY, 23);
						tCla.setTimeInMillis(tCla.getTimeInMillis() + 60 * 60 * 1000);
					}
				}
				tCla.set(Calendar.MINUTE, 0);

//				tCla.set(Calendar.HOUR_OF_DAY, 20);
//				tCla.set(Calendar.MINUTE, 0);
//				tCla.setFirstDayOfWeek(Calendar.SUNDAY);
//				tCla.set(Calendar.DAY_OF_WEEK, tCla.getFirstDayOfWeek());
//
//				if (!(tDay == tCla.get(Calendar.DATE) && tHour == tCla.get(Calendar.HOUR_OF_DAY) && tMinute == tCla.get(Calendar.MINUTE)))
//				{
//					tCla.setTime(getNextDate(pType, tCla.getTime(), null));
//				}




				break;
			}
			case CP_1hour:
			{
				if(tMinute == 0 && tHour == 0)
				{

				}else
				{
					tCla.add(Calendar.DAY_OF_MONTH,1);
					tCla.set(Calendar.MINUTE, 0);
					tCla.set(Calendar.HOUR_OF_DAY, 0);
				}

//				tCla.set(Calendar.HOUR_OF_DAY, 20);
//				tCla.set(Calendar.MINUTE, 0);
//				tCla.setFirstDayOfWeek(Calendar.SUNDAY);
//				tCla.set(Calendar.DAY_OF_WEEK, tCla.getFirstDayOfWeek());
//
//				if (!(tDay == tCla.get(Calendar.DATE) && tHour == tCla.get(Calendar.HOUR_OF_DAY) && tMinute == tCla.get(Calendar.MINUTE)))
//				{
//					tCla.setTime(getNextDate(pType, tCla.getTime(), null));
//				}

				break;
			}
			case CP_4hour:
			{
				if(tMinute == 0 && tHour == 0 && tDay == 1)
				{

				}else
				{
					tCla.add(Calendar.MONTH,1);
					tCla.set(Calendar.MINUTE, 0);
					tCla.set(Calendar.HOUR_OF_DAY, 0);
					tCla.set(Calendar.DAY_OF_MONTH, 1);
				}

//				tCla.set(Calendar.HOUR_OF_DAY, 0);
//				tCla.set(Calendar.MINUTE, 0);
//				if (tDay != 1)
//				{
//					tCla.set(Calendar.DATE, 1);
//					tCla.setTime(getNextDate(pType, tCla.getTime(), null));
//				}
				break;
			}
			case CP_1day:
			{
				if(tMinute == 0 && tHour == 0 && tDay == 1)
				{

				}else
				{
					tCla.add(Calendar.MONTH,1);
					tCla.set(Calendar.MINUTE, 0);
					tCla.set(Calendar.HOUR_OF_DAY, 0);
					tCla.set(Calendar.DAY_OF_MONTH, 1);
				}

//				tCla.set(Calendar.HOUR_OF_DAY, 0);
//				tCla.set(Calendar.MINUTE, 0);
//				if (tDay != 1)
//				{
//					tCla.set(Calendar.DATE, 1);
//					tCla.setTime(getNextDate(pType, tCla.getTime(), null));
//				}
				break;
			}
			case CP_1week:
			{

//				if(tMinute == 0 && tHour == 0 && tDay == 1 && tMonth == 0)
//				{
//
//				}else
//				{
//					tCla.add(Calendar.YEAR,1);
//					tCla.set(Calendar.MINUTE, 0);
//					tCla.set(Calendar.HOUR_OF_DAY, 0);
//					tCla.set(Calendar.DAY_OF_MONTH, 1);
//					tCla.set(Calendar.MONTH, 0);
//				}

				tCla.set(Calendar.HOUR_OF_DAY, 0);
				tCla.set(Calendar.MINUTE, 0);
				if (tMonth >= 0 && tMonth < 6)
				{
					if (tMonth == 0 && tDay == 1)
					{
						tCla.set(Calendar.MONTH, 0);
						tCla.set(Calendar.DATE, 1);
					}
					else
					{
						tCla.set(Calendar.DATE, 1);
						tCla.set(Calendar.MONTH, 6);
					}
				}

				else if (tMonth >= 6 && tMonth < 12)
				{
					if (tMonth == 6 && tDay == 1)
					{
						tCla.set(Calendar.MONTH, 6);
						tCla.set(Calendar.DATE, 1);
					}
					else
					{
						tCla.set(Calendar.DATE, 1);
						tCla.set(Calendar.MONTH, 12);
					}
				}
				break;
			}
			case CP_1month:
			{
				tCla.set(Calendar.YEAR, tYear + 1);
				tCla.set(Calendar.MONTH, 0);
				tCla.set(Calendar.DATE, 1);
				tCla.set(Calendar.HOUR_OF_DAY, 0);
				tCla.set(Calendar.MINUTE, 0);
				break;
			}
		}

		tResult = tCla.getTime();
		return tResult;
	}

	/**
	 * 获取一个tag坐标包括多少数据条数
	 *
	 * @return
	 */
	public int getTagXCount()
	{
		return mXTagValueCount;
	}

	public void setTagXCount(int num)
	{
		mXTagValueCount = num;
	}

	private Date checkNextDate(ChartData.ChartPeroid pType, Date pDate)
	{
		Date tResult = null;
		int intervalNum = 1;//时间间隔

		if (m_ScaleFactor <= 0.5f && m_ScaleFactor > 0.33f)
		{
			intervalNum = intervalNum * 2;
		}
		else if (m_ScaleFactor <= 0.33f && m_ScaleFactor > 0.25f)
		{
			intervalNum = intervalNum * 3;
		}
		else if (m_ScaleFactor == 0.25f)
		{
			intervalNum = intervalNum * 4;
		}

//		FDTLogUtils.d("tingchart4","checkNextDate intervalNum"+intervalNum+" m_ScaleFactor"+m_ScaleFactor);

//		if(pType == CP_1min || pType == CP_5min || pType == CP_15min || pType == CP_1hour )
//		{
//			intervalNum = intervalNum * 2;
//		}


		Calendar tCla = Calendar.getInstance();
		tCla.setTime(pDate);
		int tYear = tCla.get(Calendar.YEAR);
		int tMonth = tCla.get(Calendar.MONTH);
		int tDay = tCla.get(Calendar.DATE);
		int tHour = tCla.get(Calendar.HOUR_OF_DAY);
		int tMinute = tCla.get(Calendar.MINUTE);
		switch (pType)
		{
			case CP_1min:
			{
				if (tMinute == 0)
				{
					tMinute = 0;
				}
				else if (tMinute >= 0 && tMinute < 15)
				{
					tMinute = 15;
				}
				else if (tMinute >= 0 && tMinute < 30)
				{
					tMinute = 30;
				}
				else if (tMinute >= 30 && tMinute < 45)
				{
					tMinute = 45;
				}
				else
				{
					tMinute = 0;
					tCla.add(Calendar.HOUR_OF_DAY, 1);
				}

				tCla.set(Calendar.MINUTE, tMinute);
				break;
			}
			case CP_5min:
			{

//
//				if (tMinute == 0)
//				{
//					tMinute = 0;
//				}
				if (tMinute == 0)
				{
					tMinute = 0;
				}
				else if(tMinute <= 60)
				{
					tMinute = 0;
					tCla.add(Calendar.HOUR_OF_DAY, 1);
				}



				tCla.set(Calendar.MINUTE, 0);

				break;
			}
			case CP_15min:
			{
				if (tHour >= 0 && tHour < 6)
				{
					if (tHour == 0 && tMinute == 0)
					{
						tCla.set(Calendar.HOUR_OF_DAY, 0);
					}
					else
					{
						tCla.set(Calendar.HOUR_OF_DAY, 6);
					}
				}
				else if (tHour >= 6 && tHour < 12)
				{
					if (tHour == 6 && tMinute == 0)
					{
						tCla.set(Calendar.HOUR_OF_DAY, 6);
					}
					else
					{
						tCla.set(Calendar.HOUR_OF_DAY, 12);
					}
				}
				else if (tHour >= 12 && tHour < 18)
				{
					if (tHour == 12 && tMinute == 0)
					{
						tCla.set(Calendar.HOUR_OF_DAY, 12);
					}
					else
					{
						tCla.set(Calendar.HOUR_OF_DAY, 18);
					}
				}
				else
				{
					if (tHour == 18 && tMinute == 0)
					{
						tCla.set(Calendar.HOUR_OF_DAY, 18);
					}
					else
					{
						tCla.set(Calendar.HOUR_OF_DAY, 23);
						tCla.setTimeInMillis(tCla.getTimeInMillis() + 60 * 60 * 1000);
					}
				}
				tCla.set(Calendar.MINUTE, 0);
				break;
			}
			case CP_30min:
			{
				if (tHour >= 0 && tHour < 6)
				{
					if (tHour == 0 && tMinute == 0)
					{
						tCla.set(Calendar.HOUR_OF_DAY, 0);
					}
					else
					{
						tCla.set(Calendar.HOUR_OF_DAY, 6);
					}
				}
				else if (tHour >= 6 && tHour < 12)
				{
					if (tHour == 6 && tMinute == 0)
					{
						tCla.set(Calendar.HOUR_OF_DAY, 6);
					}
					else
					{
						tCla.set(Calendar.HOUR_OF_DAY, 12);
					}
				}
				else if (tHour >= 12 && tHour < 18)
				{
					if (tHour == 12 && tMinute == 0)
					{
						tCla.set(Calendar.HOUR_OF_DAY, 12);
					}
					else
					{
						tCla.set(Calendar.HOUR_OF_DAY, 18);
					}
				}
				else
				{
					if (tHour == 18 && tMinute == 0)
					{
						tCla.set(Calendar.HOUR_OF_DAY, 18);
					}
					else
					{
						tCla.set(Calendar.HOUR_OF_DAY, 23);
						tCla.setTimeInMillis(tCla.getTimeInMillis() + 60 * 60 * 1000);
					}
				}
				tCla.set(Calendar.MINUTE, 0);

//				int tHour = tCla.get(Calendar.HOUR_OF_DAY);
//				int tMinute = tCla.get(Calendar.MINUTE);
//
//				if (!(tHour == 0 && tMinute == 0))
//				{
//					tCla.set(Calendar.HOUR_OF_DAY, 23);
//					tCla.set(Calendar.MINUTE, 0);
//					tCla.setTimeInMillis(tCla.getTimeInMillis() + 60 * 60 * 1000);
//				}



				break;
			}
			case CP_1hour:
			{

					tCla.add(Calendar.DAY_OF_MONTH,1);
					tCla.set(Calendar.MINUTE, 0);
					tCla.set(Calendar.HOUR_OF_DAY, 0);
//				int tHour = tCla.get(Calendar.HOUR_OF_DAY);
//				int tMinute = tCla.get(Calendar.MINUTE);
//
//				if (!(tHour == 0 && tMinute == 0))
//				{
//					tCla.set(Calendar.HOUR_OF_DAY, 23);
//					tCla.set(Calendar.MINUTE, 0);
//					tCla.setTimeInMillis(tCla.getTimeInMillis() + 60 * 60 * 1000);
//				}


//				pDate.setTime(pDate.getTime() + intervalNum * 7L * 24 * 60 * 60 * 1000);

				break;
			}
			case CP_4hour:
			{
				tCla.set(Calendar.DATE, 1);

				if (tMonth + intervalNum >= 12)//超过一年的加一年
				{
					tCla.set(Calendar.MONTH, (tMonth + intervalNum) % 12);
					tCla.set(Calendar.YEAR, tYear + 1);
				}
				else
				{
					tCla.set(Calendar.MONTH, tMonth + intervalNum);
				}
				break;
			}
			case CP_1day:
			{

				tCla.set(Calendar.DATE, 1);

				if (tMonth + intervalNum >= 12)//超过一年的加一年
				{
					tCla.set(Calendar.MONTH, (tMonth + intervalNum) % 12);
					tCla.set(Calendar.YEAR, tYear + 1);
				}
				else
				{
					tCla.set(Calendar.MONTH, tMonth + intervalNum);
				}
				break;
			}
			case CP_1week:
			{
				tCla.set(Calendar.DATE, 1);

				if (tMonth + 6 * intervalNum >= 12)
				{
					tCla.set(Calendar.MONTH, (tMonth + 6 * intervalNum)%12);
					tCla.set(Calendar.YEAR, tYear + (tMonth + 6 * intervalNum)/12);
				}
				else
				{
					tCla.set(Calendar.MONTH, tMonth + 6 * intervalNum);
				}

				break;
			}
			case CP_1month:
			{
				tCla.set(Calendar.YEAR, tYear + intervalNum);
				tCla.set(Calendar.MONTH, 0);
				tCla.set(Calendar.DATE, 1);

				break;
			}
		}
		tResult = tCla.getTime();

		//FDTLogUtils.d("tingchart2","checkNextDate pdate"+pDate+"pcurrentdate"+pCurrentDate);
//		if(pDate != null && tResult.compareTo(pDate) < 0)
//		{
//			pDate = checkNextDate(pType,pDate);
//		}
		return tResult;
	}

	public Date getNextDate(ChartData.ChartPeroid pType, Date pDate)
	{
//		if (pCurrentDate == null)
//		{
//			return checkNextDate(pType, pDate,pCurrentDate);
//		}
//		else
//		{
			return checkNextDate(pType, pDate);
//			Date tDate = getFirstDate(pType, pCurrentDate);
//			if (tDate.compareTo(pCurrentDate) == 0)
//			{
//				return checkNextDate(pType, pDate);
//			}
//			else
//			{
//				return tDate;
//			}
//		}
	}

	public void setCallback(CoordinateCallback pCallback)
	{
		m_callback = pCallback;
	}

	public boolean containPoint(android.graphics.Point pPoint)
	{
		return m_rectfLayerRect.contains(pPoint.x, pPoint.y);
	}

	public void setIsShowXAxis(boolean pIsShowAxis)
	{
		m_xAxis.m_bIsShowAxis = pIsShowAxis;
	}

	public void setIsShowYAxis(boolean pIsShowAxis)
	{
		m_yAxis.m_bIsShowAxis = pIsShowAxis;
	}

	public void setIsShowXCoordinate(boolean pIsShow)
	{
		m_xAxis.m_bIsShowCoordinate = pIsShow;
	}

	public void setIsShow0LineCoordinate(boolean pIsShow)
	{
		m_yAxis.m_bIsShowLine = pIsShow;
	}

	public void setIsShowYCoordinate(boolean pIsShow)
	{
		m_yAxis.m_bIsShowCoordinate = pIsShow;
	}

	public float getXTextSize()
	{
		return m_xAxis.getTextSize();
	}

	public float getYTextSize()
	{
		return m_yAxis.getTextSize();
	}

	public boolean isCoordinateTextOuterY()
	{
		return m_callback.isCoordinateTextOuterY();
	}

	public float getScaleFactor()
	{
		return m_callback.getScaleFactor();
	}

	public void setIsShowPercent(CoinChart.AxisPercentType m_bIsShowPercentAxis)
	{
		m_yAxis.setIsShowPercent(m_bIsShowPercentAxis);
	}

	public boolean getIsShowMaxMinArrow(){
		return m_callback.isShowMaxMinArrow();
	}

	public CoinChart.AxisPercentType getIsShowPercent()
	{
		return m_yAxis.m_bIsShowPercent;
	}


	public interface CoordinateCallback
	{
		int getXTagCount();

		Layer.ChartTypeEnum getCharTypeEnum();

		Rect getPanelRect();

		boolean isSimpleMode();

		boolean isShowYesterClose();

		float getYesterClose();

		boolean isCoordinateTextOuterY();

		int getDisplayCount();

		float getScaleFactor();

		int getYTagCount();

		boolean isDrawAllData();

		List<CandleData> getAdditionMinMax();

		boolean isCOnlyShowSquareNum();

		boolean isNeedUpDownSpace();

		boolean isShowMaxMinArrow();
	}
}
